<!-- create begin -->
<div class=" create">
   
    <!-- 表单 -->   
    <section class="panel-body" >
            <form role="form" class="form-create">

                <div class="form-group col-sm-6">
                    <span>编号</span>
                    <input type="text" class="form-control" placeholder="编号" name="sn" v-model="param.sn">
                </div>

                <div class="form-group col-sm-6">
                    <span>入库类型</span>
                    <select name="supplier" class="form-control" v-model="param.type" >
                        <option value="1" >采购入库</option>
                        <option value="2" >手动入库</option>
                    </select>
                </div>

                <div class="form-group col-sm-6" v-show="param.type == 1 ">
                    <span>关联订单</span>
                    <select name="purchase" class="form-control comboSelect purchase" v-model="param.purchase">
                        {volist name="list" id="vo"}
                            <option value="{$vo.id}" >{$vo.sn}-{$vo.author}</option>
                        {/volist}
                    </select>
                </div>

                <div class="form-group col-sm-6">
                    <span>入库人</span>
                    <input type="text" class="form-control" placeholder="入库人" name="author" v-model="param.author">
                </div>

                <div class="form-group col-sm-6">
                    <span>供应商</span>
                    <select name="supplier" class="form-control comboSelect" v-model="param.supplier">
                        {volist name="supplier" id="vo"}
                            <option value="{$vo.id}" >{$vo.title}</option>
                        {/volist}
                    </select>
                </div>                

                <div class="form-group col-sm-6">
                    <span>入库日期</span>
                    <input type="text" class="form-control" placeholder="入库日期" name="in_date" id="in_date" v-model="param.in_date">
                </div>

                {include file="common/depot-table" /}

                <div class="form-group col-sm-12">
                    <button type="button" class="btn btn-sm btn btn-primary" @click="save">  保 存 </button>
                    <button type="reset" class="btn btn-sm btn-success ">  重 置 </button> 
                </div>

            </form>
    </section>

</div>
<!-- create end-->

<script>

var list = [];
var layer_index = 0;

// seajs
seajs.use(['vendor/comboSelect/jquery.combo.select','vendor/laydate/laydate'],function() {

    var data = {
        name: '名称',
        model: '型号',
        spec: '规格',
        color: '颜色',
        num: '数量',
        unit: '单位',
        price: '单价',
        count: '合计',
    };

    var create = new Vue({
        el: '.form-create',
        data: {
            data:data,
            count:0,
            list: list,
            param:{
                sn:"{:date('Ymdhis').rand(100,999)}",
                author:"{$admin_user.nickname}",
                in_date: "{:date('Y-m-d',time() )}",
                supplier: "{$supplier[0]['id']}",
                data: this.list,
                count: this.count,
                purchase: this.purchase,
                type: 1
            }
        },
        mounted: function (){
            $('.comboSelect').comboSelect();
            laydate.render({
                elem: '#in_date',
                theme: '#2DCB73'
            });
            
        },
        methods: {
            save: function () {
                this.param.data = this.list;
                this.param.count = this.count;

                $.ajax({
                    url: "{:url('save')}",
                    method: 'POST',
                    dataType: 'json',
                    data: this.param,
                    success: this.callback
                });
            },
            callback: function (data) {
                if( data.error >0 ){
                    toastr.error(data.msg);
                }else{
                    toastr.success(data.msg);
                    Table.methods.refresh();
                    layer.closeAll('page');
                }
            },
            getList:function(){
                var _this = this;
                $.getJSON("{:url('purchase/getList')}?id="+this.param.purchase, function(data){
                    // console.log(data);
                    list = data;
                    _this.list = data;
                    _this.SumCount();
                });
            },
            addLine: function(){

                if( this.param.type == 2 ){
                    $.get("{:url('product/select')}", function(data) {
                        layer_index = layer.open({
                            type: 1,
                        　　title: "选择产品",
                        　　shadeClose: true,
                        　　shade: 0.4,
                            anim: 5,
                            area: ['150%','500px'],
                        　　content: data
                        });
                    });
                }

            },
            delLine:function(index){
                this.param.type == 2 && this.list.splice(index,1);
            },
            changeSum:function(index){
                this.list[index].count = this.list[index].num * this.list[index].price;
                this.SumCount();
            },
            SumCount:function(){
                this.count = 0;
                for(var index in this.list) {  
                    this.count += parseFloat( this.list[index].count );
                };  
            }

        }


    });


// $('.comboSelect').change(function(){
//     var name = $(this).attr('name');
//     var val = $(this).val();
//     console.log(val);
//     create.param[name] = val;
//     create.getList();
// });

$('.purchase').change(function(){
    var name = $(this).attr('name');
    var val = $(this).val();
    // console.log(val);
    create.param[name] = val;
    create.getList();
});




});

</script>